/*
 **************************************************************************************
 * Copyright (C) 2005-2011 UC Mobile Limited. All Rights Reserved
 * File			: AutomatedFunImpl.m
 *
 * Description	: 常用自动化主要功能接口实现
 *
 * Author		: daijb@ucweb.com
 * History		:
 *			   Creation, 2014/03/18, daijb, Create the file
 ***************************************************************************************
 **/
#if defined(UC_COMMON_AUTOMATED_TEST) || defined(UC_MONKEY_TEST)
#import "VerDefine.h"


#import "AutomatedFunImpl.h"
#import "SettingAgent.h"
#import "NSObject_Event.h"
#import "UCCmdDefine.h"
#import "stdafx.h"
#import "iStdafxMM.h"
#import "iUCEngine.h"
#import "ThemeManager.h"
#import "HttpURLCache.h"
#import "WMLManager.h"
#import "NSObject_Event.h"
#import "NBMediator.h"
#import "iUCCommon.h"
#import "ResManager.h"
#import "FullScreenController.h"
#import "IWebListManager.h"
#import "DownloadModelFactory.h"
#import "iUCViewMgr.h"

id<AutomatedFunProtocol> sharedAutomatedFuncInstance()
{
	return [AutomatedFunImpl shareInstance];
}

@implementation AutomatedFunImpl

+ (instancetype)shareInstance
{
    static AutomatedFunImpl* instance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        instance = [[AutomatedFunImpl alloc] init];
    });
    
    return instance;
}

#pragma mark - 菜单

//日夜间切换
- (void)dayAndNightSwitch
{
    [[ThemeManager sharedInstance] switchDayNightMode:YES];
}

//进入全屏/退出全屏
- (void)fullScreenSwitch
{
    [[FullScreenController shareInstance] switchScreenModeAnimated:YES];
}

//无图模式开关
- (void)noPictureMode
{
    BOOL noImage = [[SettingAgent standardSettingAgent] boolForKey:USERSET_BOOL_BASE_NO_IMAGE];
    noImage = !noImage;
    [[SettingAgent standardSettingAgent] setBool:noImage forKey:USERSET_BOOL_BASE_NO_IMAGE];
    
    id currentWebView = [getWebListManager() currentWebView];
    if (![currentWebView isHomePageOrInvalidPage] && ![currentWebView isReadingModeEnabled])
    {
        [currentWebView reload];
    }
}

- (void)speedModeSwitch
{
    isThunderModeOnOff(![[SettingAgent standardSettingAgent] isSpeedMode]);
}

- (void)switchUA
{
    int currentSel = [[SettingAgent standardSettingAgent] integerForKey:USERSET_INT_BASE_NEW_UA_INDEX];
    int newIndex = 0;
    int count = g_UAManager.getUAListCount();
    if (currentSel == count -1 )
    {
        newIndex = currentSel - 1;
    }
    else
    {
        newIndex = currentSel + 1;
    }
    
    if (newIndex == count)
    {
        newIndex = newIndex - 1;
    }
    
    //change UA Index
    [[SettingAgent standardSettingAgent] setInteger:newIndex forKey:USERSET_INT_BASE_NEW_UA_INDEX];
    [[SettingAgent standardSettingAgent] setInteger:(int)TYPESET_TYPE_ZOOM forKey:USERSET_INT_BASE_TYPESETTYPE];
    //iPhone UA <---> Chrome UA
    iUCCommon::applyWebkitUserAgentIfUsingWebkit();
}
//退出浏览器
- (void)exitBrowser
{
    MUISubmitCmd(UC_CMD_IPHONE_SYSTEM_QUIT,(void *)1,NULL);
}

#pragma mark - 设置

//清除网页缓存
- (void)clearWebPageCache
{
    [self clearCacheAndUpdateCacheCell:YES];
}

//广告过滤开关
- (void)adBlockSetting
{
    BOOL adBlock = [[SettingAgent standardSettingAgent] boolForKey:USERSET_BOOL_ADBLOCK];
    [[SettingAgent standardSettingAgent] setBool:!adBlock forKey:USERSET_BOOL_ADBLOCK];
}

//云加速开关
- (void)cloudSpeedUp
{
    BOOL currentCloudSpeedupStatus = [[SettingAgent standardSettingAgent] boolForKey:USERSET_BOOL_CLOUDSPEEDUP];
    BOOL needToChangeStatus = !currentCloudSpeedupStatus;
    [[SettingAgent standardSettingAgent] setBool:needToChangeStatus forKey:USERSET_BOOL_CLOUDSPEEDUP];
    if (!needToChangeStatus)
    {
        [[SettingAgent standardSettingAgent] setBool:NO forKey:USERSET_INT_PROXY_LAYOUT_MODE];
    }
}

//精简排版开关
- (void)viewModeFillScreen
{
    BOOL isProxyAdaptive = [[SettingAgent standardSettingAgent] integerForKey:USERSET_INT_PROXY_LAYOUT_MODE];

    if(isProxyAdaptive)
    {
        isThunderModeOnOff(NO);
    }
    else
    {
        isThunderModeOnOff(YES);
    }
}

//网页预加载开关
- (void)reloadWebPage
{
    PERREAD_TYPE type = (PERREAD_TYPE)[[SettingAgent standardSettingAgent] integerForKey:USERSET_INT_PERREAD_TYPE];
    if (type == PERREAD_TYPE_DISABLE)
    {
        [[SettingAgent standardSettingAgent] setInteger:PERREAD_TYPE_ALL forKey:USERSET_INT_PERREAD_TYPE];
    }
    else
    {
        [[SettingAgent standardSettingAgent] setInteger:PERREAD_TYPE_DISABLE forKey:USERSET_INT_PERREAD_TYPE];
    }
}

//清除所有数据
- (void)clearAllData
{
    MUISubmitExt(L"ext:sercmd:delck:notip",NULL);
    [self clearCacheAndUpdateCacheCell:NO];
    MUISubmitCmd(UC_CMD_IPHONE_CLEAR_SYSTEM, NULL, NULL);
    MUISubmitCmd(UC_CMD_IPHONE_UI_LOCK_ORIENT,NULL,NULL);
    iUCCommon::applyU2UserAgentIfUsingU2();
    iUCCommon::applyWebkitUserAgentIfUsingWebkit();
    [[NBMediator sharedInstance] onClearDataWithType:NBClearCacheAll];
}

//清除本地下载任务
- (void)clearLocalDownloadTask
{
    id<DownloadModel> model = [[DownloadModelFactory sharedFactory] defaultDownloadModel];
    [model deleteAllTaskAndShouldDeleteFile:YES];
}

//恢复默认设置
- (void)restoreDefaultSetting
{
    [[FullScreenController shareInstance] exitAppFullScreenAnimated:NO];
    [[ThemeManager sharedInstance] switchToClassicMode];
    [[SettingAgent standardSettingAgent] resetALL];
    [[FullScreenController shareInstance] restoreLastScreenModeAnimated:YES];
    
    BOOL bVoiceOpen = [[SettingAgent standardSettingAgent] boolForKey:USERSET_BOOL_VOICE_INVALID];
    [[SettingAgent standardSettingAgent] setBool:bVoiceOpen forKey:USERSET_BOOL_VOICE_INVALID];
    
    MUISubmitCmd(UC_CMD_SYSTEM_RESET, NULL, NULL);
    MUISubmitCmd(UC_CMD_IPHONE_UI_LOCK_ORIENT,NULL,NULL);
    MUISubmitCmd(UC_CMD_SYS_PAGE_REFRESH_RESET,NULL,NULL);
    MUISubmitCmd(UC_CMD_IPHONE_ENABLE_PAGE_MODE, (void*)PAGE_CTRL_NOTIFY, NULL);
}

#pragma mark - misc
- (void)clearCacheAndUpdateCacheCell:(BOOL)shouldKeepU2CacheAfterLaunch
{
    ICacheEngine::GetInstance()->clearAll(!shouldKeepU2CacheAfterLaunch);
    [[HttpURLCache defaultCache] removeAllCachedResponses];
    [[SettingAgent standardSettingAgent] setBool:shouldKeepU2CacheAfterLaunch forKey:CACHE_SIZE_FORCE_TO_SHOW_EMPTY];
	[[[WMLManager sharedInstance] getWhiteList] clearWhiteList];
    [[NBMediator sharedInstance] onClearDataWithType:NBClearCacheCache];
}

@end
#endif
